<%--
  Created by IntelliJ IDEA.
  User: 123
  Date: 2022-12-21
  Time: 21:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="${pageContext.request.contextPath}/">
    <link href="css/bootstrap.min.css" rel="stylesheet"/>
    <script src="js/jquery-3.2.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <title>新增商品信息</title>
    <style>
        .father {
            width: 1000px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div class="father">
    <h1 style="text-align: center;">新增商品</h1>
    <form method="post" action="doInsert">
        <table class="table table-striped table-bordered .table-hover">
            <tr>
                <td>商品名称</td>
                <td><input type="text" class="form-control" name="cname"></td>
                <td>单价</td>
                <td><input type="text" class="form-control" name="price"></td>
                </td>
            </tr>
            <tr>
                <td>分类</td>
                <td><select class="form-control" name="classId" onchange="selectBrand()">
                </select></td>
                <td>品牌</td>
                <td><select class="form-control" name="brandId">

                </select></td>
            </tr>
            <tr>
                <td>型号</td>
                <td><input type="text" class="form-control" name="typeName"></td>
                <td>库存</td>
                <td><input type="text" class="form-control" name="num"></td>
            </tr>
            <tr>
                <td colspan="4" style="text-align: center">
                    <button class="btn btn-default" type="submit">提交</button>
                    <button class="btn btn-default" type="reset">重置</button>
                </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
<script>
    onload = function () {
        onloadClassName();
    }

    function onloadClassName() {
        $.getJSON("api/className/selectAll", function (classNameList) {
            $(classNameList).each(function () {
                $(`[name='classId']`).append(` <option value="` + this.classId + `">` + this.className + `</option>`)
            })
        })
    }

    function selectBrand() {
        $(`[name='brandId']`).empty();
        var classId = $(`[name='classId']`).val();
        $.getJSON(`api/brand/selectAll/` + classId + ``, function (brandList) {
            $(brandList).each(function () {
                $(`[name='brandId']`).append(` <option value="` + this.brandId + `">` + this.brandName + `</option>`)
            })
        })
    }
</script>
